package com.lty.security;

import com.lty.model.SysUser;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Collection;

/**
 * 自定义用户信息
 */
@Data
@EqualsAndHashCode
@Accessors(chain = true) // 实现链式set方法
public class SecurityUserDetails extends SysUser implements UserDetails {

    // 权限列表
    private Collection<? extends GrantedAuthority> authorities;

    public SecurityUserDetails(String userName, Collection<? extends GrantedAuthority> authorities) {
        // TODO 这里可以从数据库中查询用户信息
        this.setUsername(userName);
        String encode = new BCryptPasswordEncoder().encode("123456");
        this.setPassword(encode);
        this.setAuthorities(authorities);
    }

    /**
     * 下面这些都返回true
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
